package com.zhengsr.wanandroid.ui.main.adapter;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zhengsr.corelib.bean.CollectBean;
import com.zhengsr.corelib.bean.Tags;
import com.zhengsr.wanandroid.R;

import java.util.List;

/**
 * Created by zhengshaorui
 * time: 2018/9/1
 */

public class HomeAdapter extends BaseQuickAdapter<CollectBean,BaseViewHolder> {

    private boolean isCollected = false;

    public HomeAdapter(int layoutResId, @Nullable List<CollectBean> data) {
        super(layoutResId, data);
    }

    public void setCollected(boolean collected) {
        isCollected = collected;
    }

    public void notifyItem(int position, CollectBean item){

    }

    @Override
    protected void convert(BaseViewHolder helper, CollectBean item) {
        String msg = null;
        if (!TextUtils.isEmpty(item.getSuperChapterName())){
            msg = item.getSuperChapterName()+"/"+item.getChapterName();
        }else{
            msg = item.getChapterName();
        }
        helper.setText(R.id.item_article_author,item.getAuthor())
                .setText(R.id.item_article_chapterName, msg)
                .setText(R.id.item_article_title,item.getTitle())
                .setText(R.id.item_article_niceDate,item.getNiceDate())
                .addOnClickListener(R.id.item_search_pager_like_iv);

        if (item.isCollect() || isCollected) {
            helper.setImageResource(R.id.item_search_pager_like_iv, R.drawable.icon_like_article_select);
        } else {
            helper.setImageResource(R.id.item_search_pager_like_iv, R.drawable.icon_like_article_not_selected);
        }

        configTag(helper,item);
        configNiceData(helper,item);

    }



    /**
     * 配置项目时间
     * @param helper
     * @param item
     */
    private void configNiceData(BaseViewHolder helper, CollectBean item) {
        String niceData = item.getNiceDate();
        TextView niceDataTv = helper.getView(R.id.item_article_tag_green_tv);
        if (!TextUtils.isEmpty(niceData)){
            if (niceData.contains("2天")){
                niceDataTv.setVisibility(View.GONE);
            }else{
                niceDataTv.setVisibility(View.VISIBLE);
                niceDataTv.setText("新");
            }
        }
    }

    /**
     * 判断是否要产生项目的 tag
     * @param helper
     * @param item
     */
    private void configTag(BaseViewHolder helper, CollectBean item) {
        List<Tags> tags = item.getTags();
        TextView redText = helper.getView(R.id.item_article_tag_red_tv);
        if (tags != null && tags.size() > 0){
            redText.setVisibility(View.VISIBLE);
            redText.setText(tags.get(0).getName());
        }
    }
}
